#!/usr/bin/env python2.5
# encoding: utf-8

init()
from StreamLoader import *

logFile = "markerclock/log_studied.csv"

print """
----
Starting IMPORT of %s
----
"""%logFile

CSVMillisecondTimeStreamLoader(newStream, logFile).run()

newFilterStream(
    "CorrectedMvtJSlow", "MvtJ",
    valueFilter=lambda startTime, endTime, value: value if startTime > 1181309400 else value * 450)

duplicateStream("CorrectedMvtJSlow", "CorrectedMvtJ")

newFilterStream(
    "CorrectedMvtL", "MvtL",
    valueFilter=lambda startTime, endTime, value: value if startTime > 1181309400 else value * 450)

def aggregationStartTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    aggregationStartTime = int(startTime / 86400) * 86400
    context['result'] = aggregationStartTime

def aggregationEndTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime

    context['result'] = max(aggregationStartTime + 86400, int(endTime / 86400) * 86400)

def aggregationValueIterator(context, startTime, endTime, value):
    if value > 0:
        context['result'] = context['result'] + endTime - startTime if context['result'] != None else endTime - startTime

def aggregationValueIterator2(context, startTime, endTime, value):
    context['result'] = context['result'] + value if context['result'] != None else value

def aggregationGrouper(startTime, endTime, value):
    return int(startTime / 86400)

newAggregationStream(
    "MvtTimePerDayForJulieSlow", "CorrectedMvtJ",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)

newAggregationStream(
    "MvtTimePerDayForLionelSlow", "CorrectedMvtL",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)

newAggregationStream(
    "MvtValuePerDayForJulieSlow", "CorrectedMvtJ",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator2, aggregationGrouper)

newAggregationStream(
    "MvtValuePerDayForLionelSlow", "CorrectedMvtL",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator2, aggregationGrouper)

duplicateStream("MvtTimePerDayForJulieSlow", "MvtTimePerDayForJulie")
duplicateStream("MvtTimePerDayForLionelSlow", "MvtTimePerDayForLionel")
duplicateStream("MvtValuePerDayForJulieSlow", "MvtValuePerDayForJulie")
duplicateStream("MvtValuePerDayForLionelSlow", "MvtValuePerDayForLionel")

from StreamExporter import *

StreamExporter(streams["MvtTimePerDayForJulie"],   "markerclock/MvtTimePerDayForJulie.csv").run()
StreamExporter(streams["MvtTimePerDayForLionel"],  "markerclock/MvtTimePerDayForLionel.csv").run()
StreamExporter(streams["MvtValuePerDayForJulie"],  "markerclock/MvtValuePerDayForJulie.csv").run()
StreamExporter(streams["MvtValuePerDayForLionel"], "markerclock/MvtValuePerDayForLionel.csv").run()

show((10, 13))